package org.budo.graph.service.impl;

import org.budo.graph.entity.DubboProvider;
import org.budo.graph.service.BudoGraphService;
import org.budo.support.application.registry.BudoApplication;
import org.budo.support.http.client.Request;
import org.budo.support.http.client.Response;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
@NoArgsConstructor
public class HttpRemoteGraphServiceImpl implements BudoGraphService {
    public static final String FROM_BUDO = "FromBudo";

    public static final String HTTP_REMOTE_GRAPH_SERVICE_IMPL = "HttpRemoteGraphServiceImpl";

    private BudoApplication budoApplication;

    public HttpRemoteGraphServiceImpl(BudoApplication budoApplication) {
        this.budoApplication = budoApplication;
    }

    @Override
    public String serviceDubboDruidStat(String url) {
        String contextPath = budoApplication.getContextPath();
        String _url = "http://" + budoApplication.getIp() + ":" + budoApplication.getPort() + (null == contextPath ? "" : contextPath) + "/budo-graph" + url;
        Request request = new Request();
        request.setUrl(_url);
        request.setHeader(FROM_BUDO, HTTP_REMOTE_GRAPH_SERVICE_IMPL);
        Response response = request.execute();

        if (null == response || null == response.getBody() || response.getBody().trim().isEmpty()) {
            log.error("#33 response of " + _url + " is empty, response headers=" + response.getHeaders());
            return "response of " + _url + " is empty, response headers=" + response.getHeaders();
        }

        log.debug("#32 _url={}, response={}" + _url, response);
        return response.getBody();
    }

    @Override
    public DubboProvider dubboProviderMetadata() {
        throw new RuntimeException();
    }

    @Override
    public String getApplicationName() {
        throw new RuntimeException();
    }
}